/*
 * Copyright (C), 2002-2017, 苏宁易购电子商务有限公司
 * FileName: MemberDaoImpl.java
 * Author:   15050536
 * Date:     2017年3月24日 下午6:25:22
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.suning.sawp.member.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.suning.sawp.member.dao.ActivityDao;
import com.suning.sawp.member.dto.Activity;
import com.suning.sawp.member.dto.ActivityStore;
import com.suning.sawp.member.dto.CustomerDetail;
import com.suning.sawp.member.dto.ExpandEmployee;
import com.suning.sawp.member.dto.MemCustomer;
import com.suning.sawp.member.dto.StoreCate;
import com.suning.sawp.member.dto.StoreExpand;
import com.suning.sawp.member.vo.ActivityListVo;
import com.suning.sawp.member.vo.CustomerVo;
import com.suning.sawp.member.vo.MemberExtensionStatisVo;
import com.suning.sawp.member.vo.StoreExpandEmployeeVo;
import com.suning.sawp.member.vo.StoreExpandVo;

/**
 * 新会员拓展 活动 dao 实现类
 *
 * @author 15050536 石键平
 */
@Repository(value = "MemActivityDao")
public class ActivityDaoImpl extends BaseDaoImpl implements ActivityDao {

	public Long countActivityNumPerMonth(String storeCode, String startTime, String endTime) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("storeCode", storeCode);
		params.put("startTime", startTime);
		params.put("endTime", endTime);
		return dalClientRead.queryForObject("activity.countActivityNumPerMonth", params, Long.class);
	}

	public boolean saveActivity(Activity activity) {
		return super.save(activity);
	}

	public boolean updateActivity(Activity activity) {
		return super.update(activity);
	}

	public boolean saveActivityStore(ActivityStore activityStore) {
		return super.save(activityStore);
	}

	public boolean updateActivityStore(ActivityStore activityStore) {
		return dalClient.execute("activity.updateActivityStore", activityStore) > 0;
	}

	public boolean batchSaveActivityStores(List<ActivityStore> activityStores) {
		return super.batchSave("activity.insertActivityStore", activityStores);
	}

	public boolean batchSaveStoreCates(List<StoreCate> storeCates) {
		return super.batchSave("activity.insertStoreCate", storeCates);
	}

	public boolean saveStoreCates(StoreCate storeCate) {
		return super.save(storeCate);
	}

	public boolean updateStoreCates(StoreCate storeCate) {
		return super.update(storeCate);
	}

	@Override
	public Activity selectActivityInfoById(String id) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		return dalClient.queryForObject("activity.selectActivityInfoById", params, Activity.class);
	}

	public ActivityStore findActivityStoreByActivityIdAndStoreCode(Long activityId, String storeCode) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("activityId", activityId);
		params.put("storeCode", storeCode);
		return dalClientRead.queryForObject("activity.findActivityStoreByActivityIdAndStoreCode", params, ActivityStore.class);
	}

	public List<ActivityStore> queryActivityStoresByActivityId(Long activityId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("activityId", activityId);
		return dalClientRead.queryForList("activity.queryActivityStoresByActivityId", params, ActivityStore.class);
	}

	public List<StoreCate> queryStoreCatesByActivityIdAndStoreCode(Long activityId, String storeCode) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("activityId", activityId);
		params.put("storeCode", storeCode);
		return dalClientRead.queryForList("activity.queryStoreCatesByActivityIdAndStoreCode", params, StoreCate.class);
	}

	public List<ActivityListVo> queryActivityList4DZ(Map<String, Object> map) {
		return dalClientRead.queryForList("activity.queryActivityList4DZ", map, ActivityListVo.class);
	}

	public List<ActivityListVo> queryActivityList4DD(Map<String, Object> map) {
		return dalClientRead.queryForList("activity.queryActivityList4DD", map, ActivityListVo.class);
	}

	public List<ActivityListVo> queryActivityList4DY(Map<String, Object> map) {
		return dalClientRead.queryForList("activity.queryActivityList4DY", map, ActivityListVo.class);
	}

	public Long countActivityList4DZ(Map<String, Object> map) {
		return dalClientRead.queryForObject("activity.countActivityList4DZ", map, Long.class);
	}

	public Long countActivityList4DD(Map<String, Object> map) {
		return dalClientRead.queryForObject("activity.countActivityList4DD", map, Long.class);
	}

	public Long countActivityList4DY(Map<String, Object> map) {
		return dalClientRead.queryForObject("activity.countActivityList4DY", map, Long.class);
	}

	public ActivityListVo findActivityStatisticByCate(Map<String, Object> map) {
		return dalClientRead.queryForObject("activity.findActivityStatisticByCate", map, ActivityListVo.class);
	}

	public List<StoreExpandVo> queryExpandStatistic4Activity(Map<String, Object> map) {
		return dalClientRead.queryForList("activity.queryExpandStatistic4Activity", map, StoreExpandVo.class);
	}

	public Long countExpandStatistic4Activity(Map<String, Object> map) {
		return dalClientRead.queryForObject("activity.countExpandStatistic4Activity", map, Long.class);
	}

	public Long countExpandByActivityIdAndStoreCode(Long activityId, String storeCode) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("activityId", activityId);
		params.put("storeCode", storeCode);
		return dalClientRead.queryForObject("activity.countExpandByActivityIdAndStoreCode", params, Long.class);
	}

	public StoreExpandVo findExpandById(Long expandId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", expandId);
		return dalClientRead.queryForObject("activity.findExpandById", params, StoreExpandVo.class);
	}

	public List<StoreExpandEmployeeVo> queryEmployeesByExpandId(Long expandId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("expandId", expandId);
		return dalClient.queryForList("activity.queryEmployeesByExpandId", params, StoreExpandEmployeeVo.class);
	}

	public boolean saveStoreExpand(StoreExpand storeExpand) {
		return super.save(storeExpand);
	}

	public boolean updateStoreExpand(StoreExpand storeExpand) {
		return super.update(storeExpand);
	}

	public boolean batchSaveExpandEmployee(List<ExpandEmployee> expandEmployees) {
		return super.batchSave("activity.insertExpandEmployee", expandEmployees);
	}

	public StoreCate findStoreCateById(Long storeCateId) {
		StoreCate sc = new StoreCate();
		sc.setId(storeCateId);
		return dalClientRead.find(StoreCate.class, sc);
	}

	public boolean deleteEmployeeByExpandId(Long expandId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("expandId", expandId);
		return dalClient.execute("activity.deleteEmployeeByExpandId", params) > 0;
	}

	public Long countCustomerByActivityId(Long activityId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("activityId", activityId);
		return dalClientRead.queryForObject("activity.countCustomerByActivityId", params, Long.class);
	}

	public Long countCustomerByExpandId(Long expandId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("expandId", expandId);
		return dalClientRead.queryForObject("activity.countCustomerByExpandId", params, Long.class);
	}

	public boolean deleteActivityById(Long activityId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("activityId", activityId);
		// 删除活动，以及相关的表
		int result = dalClient.execute("activity.deleteActivityById", params);
		dalClient.execute("activity.deleteActivityStoreByActivityId", params);
		dalClient.execute("activity.deleteStoreCateByActivityId", params);
		dalClient.execute("activity.deleteExpandByActivityId", params);
		dalClient.execute("activity.deleteExpandEmployeeByActivityId", params);
		return result > 0;
	}

	public boolean deleteExpandById(Long expandId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("expandId", expandId);
		// 删除关联的店员
		dalClient.execute("activity.deleteEmployeeByExpandId", params);
		return dalClient.execute("activity.deleteExpandById", params) > 0;
	}

	public boolean deleteStoreCateById(Long storeCateId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", storeCateId);
		return dalClient.execute("activity.deleteStoreCateById", params) > 0;
	}

	public List<ActivityListVo> queryActiveActivities(String storeCode, String status) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("storeCode", storeCode);
		params.put("status", status);
		return dalClientRead.queryForList("activity.queryActiveActivities", params, ActivityListVo.class);
	}

	public CustomerVo findCustomerByPhone(String mobile, String staffId) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("mobile", mobile);
		params.put("employeeId", staffId);
		return dalClientRead.queryForObject("activity.findCustomerByPhone", params, CustomerVo.class);
	}

	public CustomerVo findCustomerDetailByPhoneSourceEmployeeId(String mobile, String source, String employeeId) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("mobile", mobile);
		params.put("source", source);
		params.put("employeeId", employeeId);
		return dalClientRead.queryForObject("activity.findCustomerDetailByPhoneSourceEmployeeId", params, CustomerVo.class);
	}

	public boolean checkCustomerJoinActivity(String mobile, String storeCode, Long activityId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("mobile", mobile);
		params.put("storeCode", storeCode);
		params.put("activityId", activityId);
		return dalClient.queryForObject("activity.checkCustomerJoinActivity", params, Long.class) > 0;
	}

	public List<Map<String, Object>> queryIntentCateByCustDetailId(String custDetailId) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("custDetailId", custDetailId);
		return dalClientRead.queryForList("activity.queryIntentCateByCustDetailId", params);
	}

	public List<Map<String, Object>> queryIntentCmmdtyByCustDetailId(String custDetailId) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("custDetailId", custDetailId);
		return dalClientRead.queryForList("activity.queryIntentCmmdtyByCustDetailId", params);
	}

	public MemberExtensionStatisVo findPlaceStatistics4Emp(Long activityId, String storeCode, String employeeId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("activityId", activityId);
		params.put("storeCode", storeCode);
		params.put("employeeId", employeeId);
		return dalClientRead.queryForObject("activity.findPlaceStatistics4Emp", params, MemberExtensionStatisVo.class);
	}

	public List<StoreExpandVo> queryPlaceList4Emp(Long activityId, String storeCode, String employeeId, int startIndex, int pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("activityId", activityId);
		params.put("storeCode", storeCode);
		params.put("employeeId", employeeId);
		params.put("startIndex", startIndex);
		params.put("pageSize", pageSize);
		return dalClientRead.queryForList("activity.queryPlaceList4Emp", params, StoreExpandVo.class);
	}

	public Long countPlaceList4Emp(Long activityId, String storeCode, String employeeId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("activityId", activityId);
		params.put("storeCode", storeCode);
		params.put("employeeId", employeeId);
		return dalClientRead.queryForObject("activity.countPlaceList4Emp", params, Long.class);
	}

	public List<Activity> queryActivityListByEndTime(String endTime) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("endTime", endTime);
		return dalClientRead.queryForList("activity.queryActivityListByEndTime", params, Activity.class);
	}

	public void delSaleByActivityId(Long activityId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("activityId", activityId);
		dalClient.execute("activity.delSaleByActivityId", params);
	}

	public List<CustomerVo> queryNoMemberCust() {
		return dalClientRead.queryForList("activity.queryNoMemberCust", null, CustomerVo.class);
	}

	public void moveOldCust2New(String createTime) {
		Map<String, Object> params = new HashMap<String, Object>();
		if (StringUtils.isNotEmpty(createTime)) {
			params.put("createTime", createTime);
		}
		dalClient.execute("activity.moveOldCust2New", params);
	}

	/**
	 * 将客户 详情表的数据，转到客户列表表中
	 */
	public void moveDetailToList(String createTime) {
		Map<String, Object> params = new HashMap<String, Object>();
		if (StringUtils.isNotEmpty(createTime)) {
			params.put("createTime", createTime);
		}
		dalClient.execute("activity.moveDetailToList", params);
	}

	public CustomerDetail queryCustomerDetailByParams(String custNo, String employeeId, String source) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("custNo", custNo);
		params.put("employeeId", employeeId);
		params.put("source", source);
		return dalClient.queryForObject("activity.queryCustomerDetailByParams", params, CustomerDetail.class);
	}

	public MemCustomer queryCustomerByParams(String custNo, String employeeId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("custNo", custNo);
		params.put("employeeId", employeeId);
		return dalClient.queryForObject("activity.queryCustomerByParams", params, MemCustomer.class);

	}
}
